package com.cloudxingkong.dianyinke.SlideTimerShaft;

/**
 * Created by ningfei on 2016/5/26.
 */
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.format.DateUtils;

import java.util.Calendar;
public class YMDDatePickerDialog extends AlertDialog implements OnClickListener {
    private YMDDatePicker ymdDatePicker;
    private Calendar mDate = Calendar.getInstance();
    private OnDateTimeSetListener mOnDateTimeSetListener;

    @SuppressWarnings("deprecation")
    public YMDDatePickerDialog(Context context, long date) {
        super(context);
        ymdDatePicker = new YMDDatePicker(context);
        setView(ymdDatePicker);
        ymdDatePicker.setOnDateTimeChangedListener(new YMDDatePicker.OnDateTimeChangedListener() {
            @Override
            public void onDateTimeChanged(YMDDatePicker view, int year, int month, int day) {
                mDate.set(Calendar.YEAR, year);
                mDate.set(Calendar.MONTH, month);
                mDate.set(Calendar.DAY_OF_MONTH, day);
                updateTitle(mDate.getTimeInMillis());
            }
        });
        setButton2("取消", (OnClickListener) null);
        setButton("确定", this);
        mDate.setTimeInMillis(date);
        updateTitle(mDate.getTimeInMillis());
    }

    public interface OnDateTimeSetListener {
        void OnDateTimeSet(AlertDialog dialog, long date);
    }

    private void updateTitle(long date) {
        int flag = DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
        setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
    }

    public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
        mOnDateTimeSetListener = callBack;
    }

    public void onClick(DialogInterface arg0, int arg1) {
        if (mOnDateTimeSetListener != null) {
            mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
        }
    }
}

